package com.usian.comment.service.impl;

import com.usian.comment.feign.ApUserClient;
import com.usian.comment.service.CommentRepayService;
import com.usian.model.comment.dto.CommentRepayDto;
import com.usian.model.comment.dto.CommentRepayLikeDto;
import com.usian.model.comment.dto.CommentRepaySaveDto;
import com.usian.model.comment.pojos.ApComment;
import com.usian.model.comment.pojos.ApCommentRepay;
import com.usian.model.comment.pojos.ApCommentRepayLike;
import com.usian.model.comment.vo.ApCommentRepayVo;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ClassName: CommentRepayServiceImpl
 * Author:   WGG
 * Date:    2022-12-2022/12/7-9:03
 * Version: 1.0
 * Description:
 */
@Service
public class CommentRepayServiceImpl implements CommentRepayService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private ApUserClient apUserClient;

    /**
     * 加载评论回复列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadCommentRepay(CommentRepayDto dto) {

        //校验参数是否为空
        if (dto.getCommentId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getMinDate()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //获取当前用户登录的id
        ApUser user = AppThreadLocalUtils.getUser();

        //判断登录状态
        if (user.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }

        //根据评论id，判断当前评论是否存在
        ApComment apComment = mongoTemplate.findById(dto.getCommentId(), ApComment.class);
        if (apComment==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }


        //定义空集合
        ArrayList<ApCommentRepayVo> apCommentRepayVos = new ArrayList<>();
        ApCommentRepayVo apCommentRepayVo = new ApCommentRepayVo();

        //根据条件获取所有当前评论数据
        List<ApCommentRepay> apCommentRepays = mongoTemplate.find(Query.query(Criteria.where("authorId").is(user.getId()).and("commentId").is(dto.getCommentId())), ApCommentRepay.class);

        for (ApCommentRepay apCommentRepay : apCommentRepays) {

            //根据条件查询当前点赞信息
            List<ApCommentRepayLike> apCommentRepayLikes = mongoTemplate.find(Query.query(Criteria.where("authorId").is(user.getId()).and("commentRepayId").is(apCommentRepay.getId())), ApCommentRepayLike.class);
            apCommentRepayVo.setOperation(apCommentRepayLikes.get(0).getOperation());

            //封装数据
            apCommentRepayVo.setLikes(apCommentRepay.getLikes());
            apCommentRepayVo.setContent(apCommentRepay.getContent());
            apCommentRepayVo.setId(apCommentRepay.getId());
            apCommentRepayVo.setAuthorId(apCommentRepay.getAuthorId());
            apCommentRepayVo.setAuthorName(apCommentRepay.getAuthorName());
            apCommentRepayVo.setCommentId(apCommentRepay.getCommentId());
            apCommentRepayVos.add(apCommentRepayVo);

        }

        //返回结果
        return ResponseResult.okResult(apCommentRepayVos);
    }


    /**
     * 保存回复内容
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveCommentRepay(CommentRepaySaveDto dto) {
        //校验参数是否为空
        if (dto.getCommentId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getContent()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //获取当前用户登录的id
        ApUser user = AppThreadLocalUtils.getUser();

        //判断登录状态
        if (user.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }

        //根据评论id，判断当前评论是否存在
        ApComment apComment = mongoTemplate.findById(dto.getCommentId(), ApComment.class);
        if (apComment==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //修改当前评论回复数
        mongoTemplate.updateMulti(
                Query.query(Criteria.where("_id").is(apComment.getId())),
        new Update().set("reply", apComment.getReply()+1),ApComment.class );

        //根据登录id获取用户当前用户信息
        ApUser apUser = apUserClient.findById(user.getId().longValue());

        //封装数据
        ApCommentRepay apCommentRepay = new ApCommentRepay();
        apCommentRepay.setAddress(null);
        apCommentRepay.setAuthorId(user.getId());
        apCommentRepay.setAuthorName(apUser.getName());
        apCommentRepay.setCommentId(dto.getCommentId());
        apCommentRepay.setContent(dto.getContent());
        apCommentRepay.setCreatedTime(new Date());
        apCommentRepay.setLatitude(null);
        apCommentRepay.setLikes(null);
        apCommentRepay.setLongitude(null);
        apCommentRepay.setUpdatedTime(new Date());

        //将数据添加到MongoDB
        mongoTemplate.insert(apCommentRepay);

        //响应结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 点赞回复内容
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveCommentRepayLike(CommentRepayLikeDto dto) {
        //校验参数是否为空
        if (dto.getCommentRepayId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getOperation()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //获取当前用户登录的id
        ApUser user = AppThreadLocalUtils.getUser();

        //判断登录状态
        if (user.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }

        //根据评论id，判断当前评论是否存在
        ApCommentRepay apCommentRepay = mongoTemplate.findById(dto.getCommentRepayId(), ApCommentRepay.class);
        if (apCommentRepay==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //查询当前点赞信息是否存在
        List<ApCommentRepayLike> apCommentRepayLikes = mongoTemplate.find(Query.query(Criteria.where("authorId").is(user.getId()).and("commentRepayId").is(dto.getCommentRepayId())), ApCommentRepayLike.class);

        ApCommentRepayLike apCommentRepayLike = new ApCommentRepayLike();
        if (apCommentRepayLikes.size()!=0){
            apCommentRepayLike = apCommentRepayLikes.get(0);
        }

        //判断点赞状态
        if (dto.getOperation()==0){

            //点赞
            //修改评论回复表中的点赞数
            mongoTemplate.updateMulti(
                    Query.query(Criteria.where("_id").is(dto.getCommentRepayId())),
                    new Update().set("likes", apCommentRepay.getLikes()==null?1:apCommentRepay.getLikes()+1), ApCommentRepay.class);

            //判断评论回复点赞表是否初创
            if (apCommentRepayLike.getId()==null){

                //添加数据
                apCommentRepayLike.setAuthorId(user.getId());
                apCommentRepayLike.setCommentRepayId(dto.getCommentRepayId());
                apCommentRepayLike.setOperation(dto.getOperation());
                mongoTemplate.insert(apCommentRepayLike);
            }else {

                //不是创修改数据
                mongoTemplate.updateMulti(
                        Query.query(Criteria.where("_id").is(apCommentRepayLike.getId())),
                new Update().set("operation", dto.getOperation()),ApCommentRepayLike.class );
            }

        }else {

            //取消点赞
            //修改评论回复表中的点赞数
            mongoTemplate.updateMulti(Query.query(
                    Criteria.where("_id").is(dto.getCommentRepayId())),
                    new Update().set("likes", apCommentRepay.getLikes()==0?null:apCommentRepay.getLikes()-1), ApCommentRepay.class);
            //修改数据
            mongoTemplate.updateMulti(
                    Query.query(Criteria.where("_id").is(apCommentRepayLike.getId())),
                    new Update().set("operation", dto.getOperation()),ApCommentRepayLike.class );


        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
